package com.slz3.business.module.memberManagement.memShoppingCar.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.memberManagement.memShoppingCar.domain.MemShoppingCar;
import com.slz3.business.module.memberManagement.memShoppingCar.domain.MemShoppingCarFactory;
import com.slz3.business.module.memberManagement.memShoppingCar.steward.MemShoppingCarSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MemShoppingCarServiceImpl implements MemShoppingCarService {

    @Override
    public MemShoppingCar save(MemShoppingCar memShoppingCar) {
        MemShoppingCar shoppingCar = Repositorys.memShoppingCar.save(MemShoppingCarFactory.initBaseFields(memShoppingCar));
        if(VE.isNotEmpty(Jedis.db0.getMap(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_INFO,shoppingCar.getSerUserId().toString(),Jedis.db0.get(RK.SHOPPING_CAR_UUID_ID.concat(":").concat(shoppingCar.getUuid().toString())))))){
            Jedis.db0.remove(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_INFO,shoppingCar.getSerUserId().toString(),Jedis.db0.get(RK.SHOPPING_CAR_UUID_ID.concat(":").concat(shoppingCar.getUuid().toString()))));
        }
        if(VE.isNotEmpty(Jedis.db0.getMap(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_ITEM,shoppingCar.getSerUserId().toString(),shoppingCar.getSerItemId().toString())))){
            Jedis.db0.remove(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_ITEM,shoppingCar.getSerUserId().toString(),shoppingCar.getSerItemId().toString()));
        }
        String carId =Jedis.db0.incrAndDecr(RK.SHOPPING_CAR_INCRI_ID,1L).toString();
        Jedis.db0.set(String.format("%s:%s",RK.SHOPPING_CAR_UUID_ID,shoppingCar.getUuid().toString()),carId);
        Jedis.db0.set(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_ITEM,shoppingCar.getSerUserId().toString(),shoppingCar.getSerItemId().toString()),Steward.obj2Map(shoppingCar));
        Jedis.db0.set(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_INFO,shoppingCar.getSerUserId().toString(),carId),Steward.obj2Map(shoppingCar));
        Jedis.db0.rpush(String.format("%s:%s",RK.SHOPPTING_CAR_INFO,shoppingCar.getSerUserId().toString()),carId);
        return shoppingCar;
    }

    @Override
    public Page<MemShoppingCar> page(MultiValueMap<String, String> params) {
        Page<MemShoppingCar> memShoppingCarPage = Repositorys.memShoppingCar.findAll(
                MemShoppingCarSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memShoppingCarPage;
    }

    @Override
    public MemShoppingCar modify(MemShoppingCar memShoppingCarNew) {
        MemShoppingCar memShoppingCarDB =Repositorys.memShoppingCar.saveAndFlush(MemShoppingCarFactory.initBaseFields(memShoppingCarNew));
        if(VE.isNotEmpty(Jedis.db0.getMap(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_INFO,memShoppingCarDB.getSerUserId().toString(),Jedis.db0.get(RK.SHOPPING_CAR_UUID_ID.concat(":").concat(memShoppingCarDB.getUuid().toString())))))){
            Jedis.db0.remove(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_INFO,memShoppingCarDB.getSerUserId().toString(),Jedis.db0.get(RK.SHOPPING_CAR_UUID_ID.concat(":").concat(memShoppingCarDB.getUuid().toString()))));
        }
        Jedis.db0.set(String.format("%s:%s:%s",RK.USER_SHOPPTING_CAR_INFO,memShoppingCarDB.getSerUserId().toString(),Jedis.db0.get(RK.SHOPPING_CAR_UUID_ID.concat(":").concat(memShoppingCarDB.getUuid().toString()))),Steward.obj2Map(memShoppingCarDB));
        return memShoppingCarDB;
    }

    @Override
    public Optional<MemShoppingCar> single(UUID uuid) {
        return Repositorys.memShoppingCar.findById(uuid);
    }

    @Override
    public List<MemShoppingCar> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memShoppingCar.findAllById(uuids);
    }

    @Override
    public List<MemShoppingCar> delete(List<MemShoppingCar> memShoppingCarList) {
        memShoppingCarList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemShoppingCarFactory.initBaseFields(item);
            Jedis.db0.removeFromList(String.format("%s:%s",RK.SHOPPTING_CAR_INFO,item.getSerUserId().toString()),0,Jedis.db0.get(String.format("%s:%s",RK.SHOPPING_CAR_UUID_ID,item.getUuid().toString())));
            Jedis.db0.remove(RK.USER_SHOPPTING_CAR_INFO.concat(":").concat(item.getSerUserId().toString()).concat(":").concat(Jedis.db0.get(String.format("%s:%s",RK.SHOPPING_CAR_UUID_ID,item.getUuid().toString()))));
        });

        return Services.memShoppingCarTransactional.batchModify(memShoppingCarList);
    }

}
